package com.example.pattern.singleton;

/**
 * 存在线程安全隐患
 */
public class LazySimpleSingleton {

    private static LazySimpleSingleton singleton = null;

    private LazySimpleSingleton() {

    }

    public static LazySimpleSingleton getInstance() {
        if (singleton == null) {
            singleton = new LazySimpleSingleton();
        }
        return singleton;
    }
}

class ExecutorThread implements Runnable {

    @Override
    public void run() {
        LazySimpleSingleton instance = LazySimpleSingleton.getInstance();
        System.out.println(Thread.currentThread().getName() + ":" + instance);
    }
}

class Main {
    /**
     * 可以使用idea线程模式调试
     */
    public static void main(String[] args) {
        //第一个线程验证为 null，第二个线程也验证为 null
        Thread t1 = new Thread(new ExecutorThread());
        Thread t2 = new Thread(new ExecutorThread());
        t1.start();
        t2.start();
        System.out.println("End");
        //End
        //Thread-1:com.example.LazySimpleSingleton@202882e1
        //Thread-0:com.example.LazySimpleSingleton@2878954d
    }
}
